﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Services;
using Maticsoft.DBUtility;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
using System.Runtime.InteropServices;

namespace BOMS.WebService
{
    /// <summary>
    /// 数据服务类
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    public class DataService : System.Web.Services.WebService
    {
        //static Dictionary<string,string> _IPMAC_DICT;

        //protected static Dictionary<string, string> IpMacDict
        //{
        //    get {

        //        if (_IPMAC_DICT == null)
        //        {
        //            _IPMAC_DICT = new Dictionary<string, string>();
        //        }
        //        return _IPMAC_DICT;
        //    }
            
        //}
        [DllImport("Iphlpapi.dll")] 
        private static extern int SendARP(Int32 dest,Int32 host,ref Int64 mac,ref Int32 length); 
        [DllImport("Ws2_32.dll")] 
        private static extern Int32 inet_addr(string ip); 


        [WebMethod]
        public string SendData(string EquipIP,string EquipMac, string xmlData)
        {
            try
            {

                //if (!IpMacDict.ContainsKey(EquipMac))
                //{                   
                //    //macAddress = GetMac(EquipIP);
                //    //string ipAddress = Context.Request.ServerVariables["REMOTE_ADDR"];
                //    IpMacDict.Add(EquipMac, EquipIP);
                //} 
                DateTime dtm = DateTime.Now;
                DbHelperSQL.connectionString = ConfigurationManager.ConnectionStrings["Conn"].ToString();
                //DbHelperSQL.ExecuteSql("exec sp_sub_LoadLogData '" + EquipMac + "','" + dtm.ToString() + "','" + xmlData + "'");
                SqlParameter[] parms = new SqlParameter[4];
                (parms[0] = new SqlParameter("@Mac", SqlDbType.NVarChar, 40)).Value = EquipMac;
                (parms[1] = new SqlParameter("@IP", SqlDbType.NVarChar, 40)).Value = EquipIP;
                (parms[2] = new SqlParameter("@dtm", SqlDbType.DateTime)).Value = dtm;
                (parms[3] = new SqlParameter("@xmldata", SqlDbType.NVarChar, -1)).Value = xmlData;
                SqlDataReader dr = DbHelperSQL.RunProcedure("sp_sub_LoadLogData", parms);
                dr.Dispose();
                dr.Close();
                return "OK";

            }
            catch (Exception err)
            {
                return err.Message;
            }

        }
        [WebMethod]
        public string GetMac(string CustomerIP)
        {
            Int32 ldest = inet_addr(CustomerIP);//目的地的ip
           // Int32 lhost= Net_addr(HostIP);//本地的ip 
            Int64 macinfo=new Int64(); 
            Int32 len=6; 
            //int len=macinfo.Length; 
            SendARP(ldest,0,ref macinfo, ref len); 
            string originalMACAddress = Convert.ToString(macinfo,16); 

            if (originalMACAddress.Length<12) 
            { 
                originalMACAddress = originalMACAddress.PadLeft(12,'0'); 
            }
            string macAddress; 
            if(originalMACAddress!="0000" && originalMACAddress.Length==12) 
            { 
                string mac1,mac2,mac3,mac4,mac5,mac6; 
                mac1=originalMACAddress.Substring(10,2);
                mac2=originalMACAddress.Substring(8,2); 
                mac3=originalMACAddress.Substring(6,2); 
                mac4=originalMACAddress.Substring(4,2); 
                mac5=originalMACAddress.Substring(2,2); 
                mac6=originalMACAddress.Substring(0,2); 
                macAddress=mac1+"-"+mac2+"-"+mac3+"-"+mac4+"-"+mac5+"-"+mac6; 
            } 
            else 
            { 
                macAddress=""; 
            } 
            return macAddress.ToUpper();
        }
    }
}